#include<bits/stdc++.h>
using namespace std;
const int MAXN = 101;
const int INF = 1e9;
int dis[MAXN];
int n, m, s;
int e[MAXN][MAXN];
int main() {
    //freopen("A.in", "r", stdin);
    cin >> n >> s;
    s++;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            int t;
            cin >> t;
            if (i == j) t = 0;
            else if (t == 0)t = INF;
            e[i][j] = t;
        }
    }
    fill(dis + 1, dis + n + 1, INF);
    dis[s] = 0;
    for (int k = 1; k < n; k++) {
        for (int u = 1; u <= n; u++) {
            for (int i = 1; i <= n; i++) {
                if (dis[u] > dis[i] + e[i][u]) {
                    dis[u] = dis[i] + e[i][u];
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (i == s)continue;
        if (dis[i] >= INF)cout << -1 << " ";
        else
            cout << dis[i] << " ";
    }
    cout << endl;
}